﻿using System;
using System.Collections.Generic;
using System.Web;

namespace HY.Common
{
    public class LogHelper
    {
        /// <summary>
        /// 日志类型，分文件放(需在配置文件配置相应节点)
        /// </summary>
        private enum LogType
        {
            TxtAndDB,
            OnlyTxt,
        }

        /// <summary>
        /// 日志实例字典
        /// </summary>
        private static Dictionary<string, log4net.ILog> logDic = new Dictionary<string, log4net.ILog>();
        static LogHelper()
        {
            if (HttpContext.Current != null)
                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(HttpRuntime.BinDirectory + "log4net.config"));
            else
                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));

            ////链接字符串需要加密时，可在此动态设置链接字符串
            //log4net.Repository.Hierarchy.Hierarchy hierarchy = log4net.LogManager.GetRepository() as log4net.Repository.Hierarchy.Hierarchy;
            //if (hierarchy != null && hierarchy.Configured)
            //{
            //    foreach (log4net.Appender.IAppender appender in hierarchy.GetAppenders())
            //    {
            //        if (appender is log4net.Appender.AdoNetAppender)
            //        {
            //            var adoNetAppender = (log4net.Appender.AdoNetAppender)appender;
            //            adoNetAppender.ConnectionString = "server=.;database=HY.MVC;uid=sa;pwd=123456;timeout=300;";
            //            adoNetAppender.ActivateOptions();
            //        }
            //    }
            //}

            //遍历创建所有实例
            //foreach (LogType logType in Enum.GetValues(typeof(LogType)))
            //    logDic[logType.ToString()] = log4net.LogManager.GetLogger(logType.ToString());
        }
        /// <summary>
        /// 使用时再创建对应的实例
        /// </summary>
        /// <param name="logType"></param>
        /// <returns></returns>
        private static log4net.ILog GetLogger(LogType logType)
        {
            if (logDic.ContainsKey(logType.ToString()))
                return logDic[logType.ToString()];
            else
            {
                log4net.ILog log = log4net.LogManager.GetLogger(logType.ToString());
                logDic[logType.ToString()] = log;
                return log;
            }
        }

        #region Info
        /// <summary>
        /// 同时记录到数据库和TXT文件
        /// </summary>
        /// <param name="logInfo">日志信息</param>
        public static void Info(LogInfo logInfo)
        {
            GetLogger(LogType.TxtAndDB).Info(logInfo);
        }
        /// <summary>
        /// 只记录到TXT文件
        /// </summary>
        /// <param name="message">日志信息</param>
        public static void Info(object message)
        {
            GetLogger(LogType.OnlyTxt).Info(message);
        }
        #endregion

        #region Error
        /// <summary>
        /// 同时记录到数据库和TXT文件
        /// </summary>
        /// <param name="logInfo">日志信息</param>
        /// <param name="exception">异常</param>
        public static void Error(LogInfo logInfo, Exception exception = null)
        {
            if (exception == null)
                GetLogger(LogType.TxtAndDB).Error(logInfo);
            else
                GetLogger(LogType.TxtAndDB).Error(logInfo, exception);
        }
        /// <summary>
        /// 只记录到TXT文件
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="exception">异常</param>
        public static void Error(object message, Exception exception = null)
        {
            if (exception == null)
                GetLogger(LogType.OnlyTxt).Error(message);
            else
                GetLogger(LogType.OnlyTxt).Error(message, exception);
        }
        #endregion
    }
}
